/*
 * @lc app=leetcode.cn id=1021 lang=javascript
 *
 * [1021] 删除最外层的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string}
 */
var removeOuterParentheses = function (s) {
  let arr = [];
  let stack = [];
  let str = '';
  s.split('').forEach(ch => {
    if (ch === '(') {
      stack.push('(');
      str += '(';
    } else {
      stack.pop()
      if (stack.length === 0) {
        arr.push(str.slice(1));
        str = '';
      } else {
        str += ')';
      }
    }
  })
  return arr.join('');
};
// @lc code=end

